<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class Account extends Model
{
    protected $fillable = [
        'key',
        'name',
        'token',
        'enable',
        'type'
    ];

    protected $attributes = [
        'enable' => true
    ];

    public function todayClue()
    {
        $date = Carbon::now()->format('Y-m-d');
        return $this->getClue($date);
    }

    public function yesterdayClue()
    {
        $date = Carbon::yesterday()->format('Y-m-d');
        return $this->getClue($date, $date, true);
    }

    public function getClue($start_time, $end_time = null, $all = false, $pass = false)
    {
        $end_time  = $end_time ?: $start_time;
        $page_size = 20;
        $data      = crmPullClues($this, $start_time, $end_time, 1, $page_size);
        $data['result'] = $data['status'] === 'success';

        if ($data['result']) {
            if ($all && (int)$data['count'] > $page_size) {
                $data = crmPullClues($this, $start_time, $end_time, 1, $data['count']);
            }
            saveClue($data['data'], $this->id, $this->type, $pass);
        } else {
            Log::info("{$this->name}账户获取失败,请确认账户Key和Token");
        }

        return $data;
    }
}
